package com.heatup.apt.common.dao;

import com.heatup.apt.model.api.coupon.CouponApiResultModel;
import com.heatup.apt.model.coupon.CouponDetail;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public interface CouponDetailMapper {
    @Delete({
        "delete from coupon_detail",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into coupon_detail (id, coupon_id, ",
        "coupon_code, make_time, ",
        "is_geted, get_time, ",
        "is_valid, user_id, ",
        "is_used, use_time, ",
        "use_begin_date, use_end_date)",
        "values (#{id,jdbcType=BIGINT}, #{couponId,jdbcType=INTEGER}, ",
        "#{couponCode,jdbcType=VARCHAR}, #{makeTime,jdbcType=TIMESTAMP}, ",
        "#{isGeted,jdbcType=INTEGER}, #{getTime,jdbcType=TIMESTAMP}, ",
        "#{isValid,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, ",
        "#{isUsed,jdbcType=INTEGER}, #{useTime,jdbcType=TIMESTAMP}, ",
        "#{useBeginDate,jdbcType=TIMESTAMP}, #{useEndDate,jdbcType=TIMESTAMP})"
    })
    int insert(CouponDetail record);

    int insertSelective(CouponDetail record);

    @Select({
        "select",
        "id, coupon_id, coupon_code, make_time, is_geted, get_time, is_valid, user_id, ",
        "is_used, use_time, use_begin_date, use_end_date",
        "from coupon_detail",
        "where id = #{id,jdbcType=BIGINT}"
    })
    @ResultMap("BaseResultMap")
    CouponDetail selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(CouponDetail record);

    @Update({
        "update coupon_detail",
        "set coupon_id = #{couponId,jdbcType=INTEGER},",
          "coupon_code = #{couponCode,jdbcType=VARCHAR},",
          "make_time = #{makeTime,jdbcType=TIMESTAMP},",
          "is_geted = #{isGeted,jdbcType=INTEGER},",
          "get_time = #{getTime,jdbcType=TIMESTAMP},",
          "is_valid = #{isValid,jdbcType=INTEGER},",
          "user_id = #{userId,jdbcType=INTEGER},",
          "is_used = #{isUsed,jdbcType=INTEGER},",
          "use_time = #{useTime,jdbcType=TIMESTAMP},",
          "use_begin_date = #{useBeginDate,jdbcType=TIMESTAMP},",
          "use_end_date = #{useEndDate,jdbcType=TIMESTAMP}",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(CouponDetail record);
 
    @Select({
        "select",
        "id, coupon_id, coupon_code, make_time, is_geted, get_time, is_valid, user_id, ",
        "is_used, use_time, use_begin_date, use_end_date",
        "from coupon_detail",
        "where binary coupon_code = #{code,jdbcType=VARCHAR}",
        "limit 1"
    })
    @ResultMap("BaseResultMap")
    CouponDetail selectByCouponCode(@Param("code") String code);
    
    @Select({
        "select",
        "id, coupon_id, coupon_code, make_time, is_geted, get_time, is_valid, user_id, ",
        "is_used, use_time, use_begin_date, use_end_date",
        "from coupon_detail",
        "where binary coupon_code = #{code,jdbcType=VARCHAR}",
        "and user_id = #{userId,jdbcType=INTEGER}",	        
        "order by id desc limit 1"
    })
    @ResultMap("BaseResultMap")
    CouponDetail selectByCouponCodeUserID(@Param("code") String code, @Param("userId") Integer userId);

    @Select({
        "select",
        "id, coupon_id, coupon_code, make_time, is_geted, get_time, is_valid, user_id, ",
        "is_used, use_time, use_begin_date, use_end_date",
        "from coupon_detail",
        "where coupon_id = #{couponId,jdbcType=INTEGER}",
        "and user_id = #{userId,jdbcType=INTEGER}",
        "order by id desc limit 1"
    })
    @ResultMap("BaseResultMap")
    CouponDetail selectByCouponId(@Param("couponId") Integer couponId,@Param("userId") Integer userId);

    /**
     * 查询指定优惠券领取记录详情分页列表
     *
     * @param paramsMap
     * @return 领取记录列表
     */
    List<CouponDetail> queryCouponDetailList(Map<String, Object> paramsMap);

    /**
     * 查询指定优惠券领取记录详情分页计数
     *
     * @param paramsMap
     * @return
     */
    int queryCouponDetailCount(Map<String, Object> paramsMap);

    /**
     * 查询指定优惠券领取记录计数
     *
     * @param paramsMap
     * @return 领取总计
     */
    int queryCouponGetCount(Map<String, Object> paramsMap);

    /**
     * 查询指定优惠券使用计数
     *
     * @param paramsMap
     * @return 使用总计
     */
    int queryCouponUsedCount(Map<String, Object> paramsMap);

    /**
     * 查询指定批次未使用优惠券的数量
     * @param paramsMap
     * @return
     */
    int queryCouponUnUsedCount(Map<String, Object> paramsMap);
    
    //<!--返回优惠券coupon_detail列表: 查询檬会员体验券，java定时器要用到，当时间到的时候，要自动添加为会员-->
    List<CouponDetail> queryCouponDetailListByVipType();

    
    
    /**
     * 批量插入coupoinDetails
     * @param couponDetails
     * @return
     */
    int insertCouponDetailsBatch(List<CouponDetail> couponDetails);


    /**
     * 作废未领取的优惠券
     *
     * @param id 优惠券ID
     * @return
     */
    int updateCouponToOfflineByCouponId(Integer id);
    
    /**
     * 根据优惠券批次ID获取优惠券信息
     * @param couponId
     * @return
     */
    CouponDetail queryCouponCodeByCouponId(Integer couponId);

    /**
     * 判断用户领取了多少张优惠券
     * list 是券id的集合
     * @param userId
     * @param list
     * @return
     */
    Integer queryUserHasCount(@Param("userId") Integer userId,@Param("list")List<String> list);


    /**
     * 按coupon_type返回固定使用期限的优惠券明细列表
     * @param couponType
     * @return
     */
    List<CouponDetail> queryUsedFixedDateCouponDetailListByCouponType(Integer couponType);
    
    /**
     * 根据批次Id查出用户有多少张优惠券未使用
     * @param userId 用户Id
     * @param list 优惠券批次Id
     * @param isUsed 使用状态  1:已使用，0:未使用
     * @return
     */
    Integer queryUserHasCountByUsed(@Param("userId") Integer userId,@Param("list")List<String> list,@Param("isUsed")Integer isUsed);

    /**
     * 根据用户及私教及体测课程类型查询课程相关优惠券
     * @return
     */
    CouponApiResultModel queryAboutSecretCouponDetail(@Param("userId") Integer userId, @Param("courseType") Integer courseType);
}